import js from '@eslint/js';
import vuePlugin from 'eslint-plugin-vue';
import tseslint from '@typescript-eslint/eslint-plugin';
import tsParser from '@typescript-eslint/parser'; // ← 新增导入 parser
import prettierConfig from 'eslint-config-prettier';

export default [
  // 1. JS 基础推荐规则
  js.configs.recommended,

  // 2. Vue 推荐规则（自带 vue-eslint-parser 配置）
  ...vuePlugin.configs['flat/recommended'],

  // 3. TypeScript 规则（仅 .ts 文件）
  {
    files: ['**/*.ts'],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        ecmaVersion: 2022,
        sourceType: 'module'
      }
    },
    plugins: {
      '@typescript-eslint': tseslint
    },
    rules: {
      ...tseslint.configs.recommended.rules,
      '@typescript-eslint/no-unused-vars': [
        'warn',
        {
          vars: 'all',
          args: 'after-used',
          ignoreRestSiblings: true,
          argsIgnorePattern: '^_'
        }
      ],
      'no-unused-vars': 'off'
    }
  },

  // 4. Vue + TypeScript（关键！）
  {
    files: ['**/*.vue'],
    languageOptions: {
      parser: vuePlugin.parser, // 即 vue-eslint-parser
      parserOptions: {
        parser: tsParser, // ← 委托 script 部分给 TS 解析器
        ecmaVersion: 2022,
        sourceType: 'module',
        extraFileExtensions: ['.vue']
      }
    },
    // Vue 插件已在上面引入，这里可不重复 plugins
    rules: {
      'vue/multi-word-component-names': 'off'
      // 其他 Vue 特有规则
    }
  },

  // 5. 全局通用规则（不含 parser）
  {
    files: ['**/*.{js,ts,vue}'],
    languageOptions: {
      globals: {
        Blob: 'readonly',
        document: 'readonly',
        window: 'readonly',
        localStorage: 'readonly',
        sessionStorage: 'readonly',
        navigator: 'readonly',
        location: 'readonly',
        FormData: 'readonly',
        FileReader: 'readonly',
        console: 'readonly',
        setInterval: 'readonly',
        clearInterval: 'readonly',
        setTimeout: 'readonly',
        ref: 'readonly',
        reactive: 'readonly',
        computed: 'readonly',
        watch: 'readonly',
        watchEffect: 'readonly',
        onMounted: 'readonly',
        onUnmounted: 'readonly',
        defineProps: 'readonly',
        defineEmits: 'readonly',
        defineExpose: 'readonly',
        withDefaults: 'readonly',
        onActivated: 'readonly',
        defineModel: 'readonly',
        nextTick: 'readonly',
        getCurrentInstance: 'readonly',
        resolveComponent: 'readonly',
        useRoute: 'readonly',
        useRouter: 'readonly',
        toRefs: 'readonly',
        defineComponent: 'readonly',
        onBeforeUnmount: 'readonly',
        toRaw: 'readonly'
      }
    },
    rules: {
      'no-undef': 'error',
      quotes: ['warn', 'single'],
      semi: ['warn', 'always']
    }
  },

  // 6. Prettier
  prettierConfig
];
